package cn.zyjw.service.impl;

import cn.zyjw.domain.SysRoleMenu;
import cn.zyjw.domain.SysRolePrivilege;
import cn.zyjw.service.SysRoleMenuService;
import cn.zyjw.service.SysRolePrivilegeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zyjw.mapper.SysRoleMapper;
import cn.zyjw.domain.SysRole;
import cn.zyjw.service.SysRoleService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    /**
     * 判断用户是否为超级管理员
     *
     * @param userId
     * @return
     */
    @Override
    public boolean isSuperAmdin(Long userId) {
        String code = this.baseMapper.getRoleCodeByUserId(userId);
        return StringUtils.isNotBlank(code) && "ROLE_ADMIN".equalsIgnoreCase(code);
    }

    /**
     * 批量删除权限
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean delRole(List<String> ids) {
        //删除权限
        this.removeByIds(ids);
        //删除权限关联的菜单
        sysRoleMenuService.remove(
                new LambdaQueryWrapper<SysRoleMenu>()
                        .in(SysRoleMenu::getRoleId, ids)
        );
        //删除权限关联的权限
        sysRolePrivilegeService.remove(
                new LambdaQueryWrapper<SysRolePrivilege>()
                        .in(SysRolePrivilege::getRoleId, ids)
        );
        return true;
    }
}
